package login_log

import (
	"AccessAdmin/internal/dao"
	"AccessAdmin/internal/model"
	"AccessAdmin/internal/service"
	"context"
)

type sLoginLog struct {
}

func init() {
	service.RegisterLoginLog(New())
}

func New() *sLoginLog {
	return &sLoginLog{}
}

// GetList 获取登录日志列表
func (s *sLoginLog) GetList(ctx context.Context, in model.LoginLogGetListInput) (out *model.LoginLogGetListOutput, err error) {
	out = &model.LoginLogGetListOutput{
		Page: in.Page,
		Size: in.Size,
	}
	m := dao.AuthLoginLogs.Ctx(ctx)
	if in.Username != "" {
		m = m.WhereLike(dao.AuthLoginLogs.Columns().Username, in.Username)
	}
	out.Total, err = m.Count()
	if err != nil {
		return out, err
	}
	err = m.Page(in.Page, in.Size).OrderDesc(dao.AuthLoginLogs.Columns().Id).Scan(&out.List)
	if err != nil {
		return nil, err
	}
	if len(out.List) == 0 {
		return nil, nil
	}
	return out, nil
}
